package com.xiaochen.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaochen.model.dto.h5.ProductSkuDto;
import com.xiaochen.model.entity.product.Product;
import com.xiaochen.model.entity.product.ProductDetails;
import com.xiaochen.model.entity.product.ProductSku;
import com.xiaochen.model.vo.h5.ProductItemVo;
import com.xiaochen.product.mapper.ProductDetailMapper;
import com.xiaochen.product.mapper.ProductMapper;
import com.xiaochen.product.mapper.ProductSkuMapper;
import com.xiaochen.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {


    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public List<ProductSku> selectHotProductSkuBySal() {
//        PageHelper.startPage(1,10);
        return productSkuMapper.selectHotProductSkuBySal();
    }

    /**
     * 条件分页查询
     * @param page
     * @param limit
     * @param productSkuDto
     * @return
     */
    @Override
    public PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);
        List<ProductSku> list = productSkuMapper.findByPage(productSkuDto);
        return new PageInfo<>(list);
    }

    /**
     * 根据skuId查询商品详情
     * @param skuId
     * @return
     */
    @Transactional
    @Override
    public ProductItemVo findProductBySkuId(Long skuId) {
        // 1.根据skuId获取sku信息
        ProductSku productSku = productSkuMapper.selectBySkuId(skuId);
        // 2.根据1.的sku获取productId,获取product信息
        Long productId = productSku.getProductId();
        Product product = productMapper.selectById(productId);
        // 3.根据productId获取productDetails信息
        ProductDetails productDetails = productDetailMapper.selectByProductId(productId);
        // 4.封装map集合 == 商品规格对应商品skuId信息
        // 4.0创建一个map集合用来存储数据
        HashMap<String, Object> skuSpecValueMap = new HashMap<>();
        // 4.1根据商品id获取商品的所有sku列表
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(productId);
        // 4.2将数据封装到map集合中
        for(ProductSku productsku : productSkuList){
            skuSpecValueMap.put(productsku.getSkuSpec(),productsku.getId());
        }
        // 5.将数据封装到ProductItemVo中并返回
        ProductItemVo productItemVo = new ProductItemVo();
        // 5.1:商品sku信息
        productItemVo.setProductSku(productSku);
        // 5.2:商品信息
        productItemVo.setProduct(product);
        // 5.3:商品轮播图列表(①拿到值,②根据,切割成一个字符串数组③将数组转换成List集合)
//        String sliderUrls = product.getSliderUrls(); // ①
//        String[] split = sliderUrls.split(","); // ②
//        List<String> list = Arrays.asList(split); // ③
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        // 5.4:商品详情图片列表 ---封装成list集合
//        String imageUrls = productDetails.getImageUrls(); // ①
//        String[] split = imageUrls.split(","); // ②
//        List<String> list = Arrays.asList(split); // ③
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        // 5.5:商品规格信息
        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        // 5.6:商品规格对应商品skuId信息
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);
        return productItemVo;
    }

    /**
     * 远程调用:根据skuId查询商品sku信息
     * @param skuId
     * @return
     */
    @Override
    public ProductSku getBySkuId(Long skuId) {
        return productSkuMapper.selectBySkuId(skuId);
    }
}
